﻿using System;
using System.Collections.Generic;
using MongoDB.Bson;
using MongoDB.Driver;
using Sustainalytics.Entities;
using Sustainalytics.Entities.ConnectionStrings;
using Sustainalytics.Entities.Governance;
using Sustainalytics.MongoDB.Extensions;
using Sustainalytics.Synchronization.Interface;
using System.Linq;
using Sustainalytics.Entities.RiskRatings;

namespace Sustainalytics.ClientDW.Synchronization.SyncRiskRatingsData.Helpers
{
    public class GovernancePillarsHelper
    {
        IMongoCollection<GovernanceIssue> _issueCollection;
        IMongoCollection<GovernanceComputationResult> _computationCollection;

        public GovernancePillarsHelper(
            IMongoCollection<GovernanceIssue> issueCollection,
            IMongoCollection<GovernanceComputationResult> computationCollection)
        {
            _issueCollection = issueCollection;
            _computationCollection = computationCollection;
        }

        public IEnumerable<GovernancePillars> Get(List<Guid> companyIds, IEnumerable<RiskRatingMaterialEsgIssue> riskRatingMaterialEsgIssues)
        {
            List<GovernancePillars> governancePillarsList = new List<GovernancePillars>();
            List<GovernanceIssue> governanceIssueList = _issueCollection
                .Find(Builders<GovernanceIssue>.Filter.In(x=> x.CompanyId, companyIds))
                .ToList();
            List<GovernanceComputationResult> governanceComputationResultList = _computationCollection
                .Find(Builders<GovernanceComputationResult>.Filter.In(x => x.Id, companyIds))
                .ToList();

            var riskRatingMaterialEsgIssueDictionary = riskRatingMaterialEsgIssues.Where(x=> x.Type == MaterialIssueType.BaselineIssue).ToDictionary(x => x.CompanyId);

            foreach (GovernanceIssue issue in governanceIssueList)
            {
                var governanceComputationResultForCompany = governanceComputationResultList.Find(x => x.Id == issue.CompanyId);

                if (governanceComputationResultForCompany == null || governanceComputationResultForCompany.IssuesPerformance == null)
                    continue;

                riskRatingMaterialEsgIssueDictionary.TryGetValue(issue.CompanyId, out var companyGovernanceMei);
                if (companyGovernanceMei == null)
                    continue;

                var pillar = companyGovernanceMei.Indicators.FirstOrDefault(x => x.Code == issue.Number);

                var issuesPerformance = governanceComputationResultForCompany.IssuesPerformance.Find(x => x.Number == issue.Number);
                var indicatorList = issue.Indicators.Select(x => new RiskRatingGovernanceIndicator()
                {
                    Id = x.Id,
                    Number = x.Number,
                    Name = x.Name,
                    Score = x.Score
                }).ToList();
                GovernancePillars item = new GovernancePillars
                {
                    Id = issue.Id,
                    Assessment = issuesPerformance.Assessment,
                    CompanyId = issue.CompanyId,
                    Comment = issue.Comment,
                    Indicators = indicatorList,
                    Name = issue.Name,
                    Number = issue.Number,
                    Score = pillar?.RawScore?.Value ?? issuesPerformance.Score
                };
                governancePillarsList.Add(item);
            }
            return governancePillarsList;
        }
    }
}
